import ProFormMutiLangText from '@/components/ProFormMutiLangText';
import {
  DrawerForm,
  ProFormDependency,
  ProFormSelect,
  ProFormText,
  ProFormTextArea,
} from '@ant-design/pro-form';
import type { FC } from 'react';
import { useModel } from 'umi';

type FormProps = {
  visible: boolean;
  current: API.TipModel | undefined;
  onCancel: () => void;
  onSubmit: (values: API.TipModel) => Promise<void>;
};

const Form: FC<FormProps> = (props) => {
  const { visible, current, onCancel, onSubmit } = props;
  if (!visible) {
    return null;
  }

  const { getProductOptions } = useModel('useGlobalModel', (model) => ({
    getProductOptions: model.getProductOptions,
  }));

  const productOptions = getProductOptions(false);

  return (
    <DrawerForm<API.TipModel>
      open={visible}
      autoFocusFirstInput
      width={640}
      title={`${current ? '编辑' : '添加'}提示信息`}
      initialValues={current}
      //preserve={false}
      // trigger={<>{children}</>}
      onFinish={onSubmit}
      drawerProps={{
        onClose: onCancel,
        //destroyOnClose: true,
        maskClosable: false,
      }}
    >
      <ProFormSelect
        name="productId"
        label="产品"
        options={productOptions}
        readonly={true}
      />
      <ProFormText name="name" label="名称" readonly={true} />
      <ProFormText name="paramKey" label="参数名" readonly={true} />
      <ProFormText name="paramValue" label="参数值" />
      <ProFormDependency name={['productId', 'mutiLang']}>
        {({ productId }) => {
          if (productId) {
            return (
              <>
                <ProFormMutiLangText productId={productId} />
              </>
            );
          }

          return null;
        }}
      </ProFormDependency>
      <ProFormTextArea name="remark" label="备注" />
    </DrawerForm>
  );
};

export default Form;
